package com.itboye.sunsun.luntan.fragment;

import java.util.ArrayList;
import java.util.List;

import com.itboye.sunsunhome.R;
import com.itboye.sunsun.adapter.WoDeHuiTieAdapter;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.WoDeHuiTieBean;
import com.itboye.sunsun.beans.WoDeHuiTieBean.WoDeHuiTieItemBean;
import com.itboye.sunsun.beans.WoDeZhuTiBean.ZhuTiItemBean;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.custome.XListView;
import com.itboye.sunsun.custome.XListView.IXListViewListener;
import com.itboye.sunsun.luntan.TieZiDetailActivity;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.support.BaseFragment;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;

public class WoDeHuiTieFragment extends  BaseFragment {


	private XListView xlistview;
	private int pageNum = 1;
 
	private BaseAdapter adapter;
	 List<WoDeHuiTieItemBean>dataList=new ArrayList<>();
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_wo_de_tiezi, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		xlistview.setPullRefreshEnable(true);
		xlistview.setPullLoadEnable(true);
		xlistview.setXListViewListener(new IXListViewListener() {

			@Override
			public void onRefresh() {
				pullDown();
			}

			@Override
			public void onLoadMore() {
				pullUp();
			}

		});
		// adapter = new ShopCarAdapter(getApplication(), dataList, this);
		// xlistview.setAdapter(adapter);
//		adapter=onGetAdapter();
		adapter=new WoDeHuiTieAdapter(dataList);
		xlistview.setAdapter(adapter);
		pullDown();
		
		xlistview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				position--;
				Intent intent=new Intent(getActivity(), TieZiDetailActivity.class);
				WoDeHuiTieItemBean bean=dataList.get(position);
				intent.putExtra("fid", bean.getFid());
				intent.putExtra("tid", bean.getTid());
				intent.putExtra("img", bean.getImg());
				
				intent.putExtra("author", bean.getThreadAuthor());
				intent.putExtra("author_id", bean.getThreadAuthorId());
				
				startActivity(intent);
			}
		});

	}

	private void pullDown() {

		pageNum = 1;
		MyJsonRequest.Builder<WoDeHuiTieBean> builder = new MyJsonRequest.Builder<WoDeHuiTieBean>()
				.param("uid",(String)SPUtils.get(getContext(), null, SPContants.USER_ID	, "")).apiVer("100")
				.param("page_no", "" + pageNum) 
				.typeKey("BY_BbsUserCenter_queryPost")
				.requestListener(new XRequestListener<WoDeHuiTieBean>() {

					@Override
					public void onResponse(WoDeHuiTieBean bean) {
					//	onPullDownFinish(adapter, xlistview, bean);
						pageNum++;
						xlistview.stopRefresh();
						dataList.clear();
						if (bean.getList()!=null) {
							dataList.addAll(bean.getList());
							adapter.notifyDataSetChanged();
						}

					}
				}).errorListener(new XErrorListener() {

					@Override
					
					public void onErrorResponse(Exception exception, int code, String msg) {
						xlistview.stopRefresh();
					//	onPullDownError(adapter, xlistview, exception, code, msg);
					}
				});

		MyJsonRequest<WoDeHuiTieBean> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	private void pullUp() {
		MyJsonRequest.Builder<WoDeHuiTieBean> builder = new MyJsonRequest.Builder<WoDeHuiTieBean>()
				.param("uid", (String) SPUtils.get(App.ctx, null, SPContants.USER_ID, "")).apiVer("100")
				.typeKey("BY_BbsUserCenter_queryPost")
				.param("page_no", "" + pageNum) 

				.requestListener(new XRequestListener<WoDeHuiTieBean>() {

					@Override
					public void onResponse(WoDeHuiTieBean bean) {
					//	onPullUpFinish(adapter, xlistview, bean);
						pageNum++;
						xlistview.stopLoadMore();
						if (bean.getList()!=null) {
							dataList.addAll(bean.getList());
							adapter.notifyDataSetChanged();
						}
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						xlistview.stopLoadMore();
					//	onPullUpError(adapter, xlistview, exception, code, msg);
					}
				});

		MyJsonRequest<WoDeHuiTieBean> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}
 
 
}
